package gov.jyy.xzxk.xksq.common.desensitization;

import com.example.tuomin.result.R;

import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;


public class DesensitizationContext {

    private static final ThreadLocal<Object> CONTEXT = new ThreadLocal<>();

    public static void clearContext() {
        CONTEXT.remove();
    }

    public static <T> Object getValue() {
        return CONTEXT.get();
    }

    public static void setValue(Object value) {
        CONTEXT.set(value);
    }

    public static <R> R runWithAutoClear(Function<Object, R> action) {
        try {
            return action.apply(CONTEXT.get());
        } finally {
            clearContext();
        }
    }

    public static <R> R getWithAutoClear(Function<Object, R> action) {
        try {
            return action.apply(CONTEXT.get());
        } finally {
            clearContext();
        }
    }
//
//    public static void runWithAutoClear(Runnable runnable) {
//        try {
//            runnable.run();
//        } finally {
//            CONTEXT.remove();
//        }
//    }
//
//    public static <T> T runWithAutoClear(Callable<T> callable) {
//        try {
//            try {
//                return callable.call();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        } finally {
//            CONTEXT.remove();
//        }
//    }
}
